package com.fengdi.everyday.middle;

import java.util.HashMap;

/**
 * @Description 2048. 下一个更大的数值平衡数
 * @Date 15:00
 */
public class NextBeautifulNumber {
    public int nextBeautifulNumber(int n) {
        for(int i = n + 1;; i++) {
            if(this.isBalance(i)) return i;
        }
    }

    private boolean isBalance(int n) {
        int ns = n;
        HashMap<Integer, Integer> map = new HashMap<>();

        while(ns > 0) {
            int x = ns % 10; // 该位数值
            map.put(x, map.getOrDefault(x, 0) + 1);
            ns = ns / 10;
        }

        ns = n;
        while(ns > 0) {
            int x = ns % 10;
            if(map.get(x) != x) return false;
            ns = ns / 10;
        }

        return true;
    }
}
